package com.itheima.sfbx.insurance.web;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.itheima.sfbx.framework.commons.basic.ResponseResult;
import com.itheima.sfbx.framework.commons.utils.ResponseResultBuild;
import com.itheima.sfbx.insurance.dto.SafeguardVO;
import com.itheima.sfbx.insurance.service.ISafeguardService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Api(tags = "保障项管理")
@RestController
@RequestMapping("/safeguard") // 编写的规范一点, 加上斜线
public class SafeguardController {

    @Autowired
    private ISafeguardService safeguardService;

    /**
     * @param safeguardVO 查询条件
     * @param pageNum     页号
     * @param pageSize    页大小
     * @return 返回结果
     * @description 多条件查询保障项分页
     */
    //属于方法
    @ApiOperation(value = "根据条件分页查询保障项", notes = "根据条件分页查询保障项")
    //指定swagger的方法参数有哪一些, 以后在swagger的文档上看到这些参数的具体属性
    @ApiImplicitParams(
            {
                    @ApiImplicitParam(name = "safeguardVO", value = "保障项VO对象", required = true, dataType = "SafeguardVO"),
                    @ApiImplicitParam(paramType = "path", name = "pageNum", value = "页号", example = "1", dataType = "Integer"),
                    @ApiImplicitParam(paramType = "path", name = "pageSize", value = "每页条数", example = "10", dataType = "Integer")
            }
    )
    @PostMapping("/page/{pageNum}/{pageSzie}")
    //ApiOperationParams 在查询条件对象中指定哪些属性可以展示
    @ApiOperationSupport(includeParameters = {"safeguardVO.dataState","safeguardVO.safeguardKey","safeguardVO.safeguardKeyName","safeguardVO.safeguardVal","safeguardVO.safeguardType","safeguardVO.sortNo","safeguardVO.remake"})
    public ResponseResult<Page<SafeguardVO>> findSafeguardVoPage(@RequestBody SafeguardVO safeguardVO,
                                                                 @PathVariable("pageNum") Integer pageNum,
                                                                 @PathVariable("pageSzie") Integer pageSize
    ) {
        Page<SafeguardVO> page = safeguardService.findPage(safeguardVO, pageNum, pageSize);
        //返回结果, 高度封装
        return ResponseResultBuild.successBuild(page);
    }

    /**
     * 新建 保障项
     *
     * @param safeguardVO 保障项对象
     * @return 袖带有id的保障项
     */
    @ApiOperation(value = "新建保障项", notes = "新建保障项")
    //指定swagger的方法参数有哪一些, 以后在swagger的文档上看到这些参数的具体属性
    @ApiImplicitParams(
            {
                    @ApiImplicitParam(name = "safeguardVO", value = "保障项VO对象", required = true, dataType = "SafeguardVO")
            }
    )
    //ApiOperationParams 在查询条件对象中指定哪些属性可以展示
    @ApiOperationSupport(includeParameters = {"safeguardVO.dataState","safeguardVO.safeguardKey","safeguardVO.safeguardKeyName","safeguardVO.safeguardVal","safeguardVO.safeguardType","safeguardVO.sortNo","safeguardVO.remake"})
    @PutMapping
    public ResponseResult<SafeguardVO> createSafeguard(@RequestBody SafeguardVO safeguardVO) {
        //调用service,接受返回结果
        SafeguardVO result = safeguardService.save(safeguardVO);
        return ResponseResultBuild.successBuild(result);
    }

    /**
     * 编辑 保障项
     *
     * @param safeguardVO 保障项对象
     * @return 操作结果
     */
    @ApiOperation(value = "编辑保障项", notes = "编辑保障项")
    @ApiImplicitParams(
            {
                    @ApiImplicitParam(name = "safeguardVO", value = "保障项VO对象", required = true, dataType = "SafeguardVO")
            }
    )
    @ApiOperationSupport(includeParameters = {"safeguardVO.dataState","safeguardVO.safeguardKey","safeguardVO.safeguardKeyName","safeguardVO.safeguardVal","safeguardVO.safeguardType","safeguardVO.sortNo","safeguardVO.remake"})
    @PatchMapping
    public ResponseResult<Boolean> updateSafeguard(@RequestBody SafeguardVO safeguardVO) {
        //调用service,接受返回结果
        Boolean flag = safeguardService.update(safeguardVO);
        return ResponseResultBuild.successBuild(flag);
    }

    /**
     * 删除 保障项
     *
     * @param safeguardVO 保障项对象
     * @return 操作结果
     */
    @ApiOperation(value = "删除保障项", notes = "删除保障项")
    @ApiImplicitParams(
            {
                    @ApiImplicitParam(name = "safeguardVO", value = "保障项VO对象", required = true, dataType = "SafeguardVO")
            }
    )
    @ApiOperationSupport(includeParameters = {"safeguardVO.checkedIds"})
    @DeleteMapping
    public ResponseResult<Boolean> deleteSafeguard(@RequestBody SafeguardVO safeguardVO) {
        //调用service,接受返回结果
        Boolean flag = safeguardService.delete(safeguardVO.getCheckedIds());
        return ResponseResultBuild.successBuild(flag);
    }

    /**
     * 查询保障项列表
     * @param safeguardVO 查询条件
     * @return 列表
     */
    @ApiOperation(value = "查询保障项列表", notes = "查询保障项列表")
    @ApiImplicitParams(
            {
                    @ApiImplicitParam(name = "safeguardVO", value = "保障项VO对象", required = true, dataType = "SafeguardVO")
            }
    )
    @ApiOperationSupport(includeParameters = {"safeguardVO.dataState","safeguardVO.safeguardKey","safeguardVO.safeguardKeyName","safeguardVO.safeguardVal","safeguardVO.safeguardType","safeguardVO.sortNo","safeguardVO.remake"})
    @PostMapping("/list")
    public ResponseResult<List<SafeguardVO>> findList(@RequestBody SafeguardVO safeguardVO) {
        List<SafeguardVO> list = safeguardService.findList(safeguardVO);
        return ResponseResultBuild.successBuild(list);
    }

}